#ifndef WORKLISTWIDGET_H
#define WORKLISTWIDGET_H

#include <QWidget>
#include "worklistplugin_global.h"
#include "worklistinterface.h"
class WorklistItemModel;
class QSortFilterProxyModel;
class WlistSCUThread;
class QCompleter;
class QSqlQueryModel;
class QTimer;

namespace Ui {
class WorklistWidget;
}

class WORKLISTPLUGINSHARED_EXPORT WorklistWidget : public QWidget, public WorklistInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID WorklistPlugin_iid)
    Q_INTERFACES(WorklistInterface)

public:
    explicit WorklistWidget(QWidget *parent = 0);
    ~WorklistWidget();

    QObject *getObject() { return this; }
    void setWorklistHook(WorklistProc proc, void *userData) { m_proc = proc; m_userData = userData; }
    const WorklistItem &getSelectedItem() const;

protected slots:
    void on_resetButton_clicked();
    void on_startAcqBtn_clicked();
    void on_todayButton_clicked();
    void on_thisWeekButton_clicked();
    void on_thisMonthButton_clicked();
    void on_clearButton_clicked();
    void on_searchButton_clicked(bool checked = true);
    void on_wlistTableView_activated(const QModelIndex &index);
    void on_autoUpdateCheck_toggled(bool checked);
    void on_autoUpdateSpin_valueChanged(int value);
    void onWlistThreadFinished();

    void updateAge(const QDate &date, bool enforce = false);
    void updateBirth();

    void patientSelected(const QString &name);

protected:
    void showEvent(QShowEvent *e);

private:
    void init();
    void initDatabase();
    void loadWlistScps();
    void setupComponents();
    void setupConnections();
    void enableComponents(bool yes);
    bool insertStudyToDB(const WorklistItem &item);

    Ui::WorklistWidget *ui;
    QTimer *timer;
    QSqlQueryModel *pModel;
    QCompleter *completer;
    WorklistItemModel *wlistModel;
    QSortFilterProxyModel *wlistProxyModel;
    WlistSCUThread *wlistThread;

    WorklistProc m_proc;
    void *m_userData;
};

#endif // WORKLISTWIDGET_H
